var fs = require('fs'); //导入文件模块

//同步  (等待和阻塞)
/* var fd = fs.openSync('hello.txt', "r")
console.log(fd) //3 
var content = fs.readFileSync(fd)
console.log(content) //<Buffer 68 65 6c 6c 6f 77 6f 72 6c 64>
console.log(content.toString()) //helloworld
 */
//异步
/* fs.readFile("hello.txt", {
  flag: 'r',
  encoding: 'utf-8'
}, function (err, data) {
  if (err) {
    console.log(err)
  } else {
    console.log(data)
  }
  console.log(456)
})
console.log(123) */
//123
//helloworld
//456


function fsRead(path) {
  return new Promise(function (resolve, reject) {
    fs.readFile(path, {
      flag: 'r',
      encoding: 'utf-8'
    }, function (err, data) {
      if (err) {
        //console.log(err)
        //失败执行的内容
        reject(err)
      } else {
        //console.log(data)
        //成功执行的内容
        resolve(data)
      }
     // console.log(456)
    })
  })
}
/* 
var w1 = fsRead('hello.txt')
w1.then(
  function (res) {
    console.log(res)
  }
) */
//456
//helloworld

async function ReadList() {
  var file2 = await fsRead('hello.txt')
  var file3 = await fsRead(file2.trim() + ".txt")//去掉左右两边空白结束符
  var file3Content = await fsRead(file3 + ".txt")
  console.log(file3Content)
}
ReadList();//hello第三个文件